package com.md.album.db;

import android.text.TextUtils;
import android.util.Log;

import com.md.basedpc.persistence.PHDbUtils;
import com.md.network.api.Album;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liyue
 * create 2021/4/4
 * desc 管理自建专辑DB 参照DownLoadDBManager
 */
public class SelfAlbumManager {
    public static SelfAlbumManager getIns() {
        return InsHolder.INS;
    }

    private SelfAlbumManager() {

    }

    static class InsHolder {
        private static SelfAlbumManager INS = new SelfAlbumManager();
    }

    public boolean saveAlbum(Album album) {
        boolean isSuc = false;
        if (album != null) {
            SelfAlbumDB db = new SelfAlbumDB();
            db.setAuthor(album.getAuthor());
            db.setCover(album.getCoverUrl());
            db.setId(album.getId());
            db.setDesc(album.getDesc());
            db.setName(album.getName());
            isSuc = saveAlbum(db);
        }
        return isSuc;
    }

    public void saveAudioList(SelfAlbumDB albumDB, List<SelfAudioDB> audioList) {
        saveAlbum(albumDB);
        for (SelfAudioDB audioDB : audioList) {
            saveAudio(albumDB.getId(), audioDB);
        }
    }

    /**
     * 选择最近播放、本地和专辑内的音频，然后加入专辑
     *
     * @param albumId 当前自建专辑ID
     * @param audio   选中的音频
     */
    public boolean saveAudioToAlbum(String albumId, Audio audio) {
        if ("-1".equals(albumId)) {
            return false;
        }
        SelfAlbumDB albumDB = getAlbumDBById(albumId);
        SelfAudioDB audioDB = new SelfAudioDB();
        audioDB.setAlbum_id(albumId);
        audioDB.setAlbum_name(albumDB.getName());
        audioDB.setId(audio.getId());
        audioDB.setUrl(audio.getAudioUrl());
        audioDB.setName(audio.getName());
        audioDB.setAuthor(audio.getAuthor());
        audioDB.setCover(audio.getCoverUrl());
        audioDB.setDesc(audio.getDesc());
        return saveAudioDB(albumId, audioDB);
    }


    /**
     * 保存 音频信息,配合{@link# saveAudioToAlbum(String, com.md.network.api.Audio)}使用
     *
     * @param albumId
     * @param audioDB
     */
    private boolean saveAudioDB(String albumId, SelfAudioDB audioDB) {
        boolean isSuccess = false;
        audioDB.setAlbum_id(albumId);
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioDB.getId()))
            where += " and id='" + audioDB.getId() + "'";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id='" + albumId + "'";
        try {
            List<SelfAudioDB> dbs = PHDbUtils.getInstance().query(SelfAudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                isSuccess = PHDbUtils.getInstance().insert(audioDB);
            } else {
                isSuccess = dbs.get(0) != null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }


    public SelfAlbumDB getAlbumDBById(String albumId) {
        SelfAlbumDB res = null;
        String where = " 1=1 ";
        where += " and id=" + albumId;
        try {
            List<SelfAlbumDB> list = PHDbUtils.getInstance().query(SelfAlbumDB.class, where);
            if (list != null) {
                res = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }


    public boolean update(SelfAlbumDB album) {
        boolean isSuc = false;
        try {
            isSuc = PHDbUtils.getInstance().update(album);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isSuc;
    }

    /**
     * 保存 专辑信息
     *
     * @param albumDB
     */
    private boolean saveAlbum(SelfAlbumDB albumDB) {
        boolean isSaved = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumDB.getId()))
            where += " and id='" + albumDB.getId() + "'";
        try {
            List<SelfAlbumDB> dbs = PHDbUtils.getInstance().query(SelfAlbumDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                isSaved = PHDbUtils.getInstance().insert(albumDB);
            } else {
                return true;
            }
        } catch (Exception e) {
            Log.e("SelfAlbumManager", e.toString());
        }
        return isSaved;
    }


    /**
     * 保存 音频信息
     *
     * @param audioDB
     */
    private SelfAudioDB saveAudio(String albumId, SelfAudioDB audioDB) {
        audioDB.setAlbum_id(albumId);
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioDB.getId()))
            where += " and id='" + audioDB.getId() + "'";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id='" + albumId + "'";
        try {
            List<SelfAudioDB> dbs = PHDbUtils.getInstance().query(SelfAudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                PHDbUtils.getInstance().insert(audioDB);
            } else {
                return dbs.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public boolean deleteAlbumById(String albumId) {
        boolean isDelSuccess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(albumId)) {
            where += " and id=" + albumId + "";
            try {
                isDelSuccess = PHDbUtils.getInstance().delete(SelfAlbumDB.class, where);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return isDelSuccess;
    }



    /**
     * 删除 音频信息
     *
     * @param audioId
     */
    public boolean deleteAudio(String audioId) {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(SelfAudioDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    public boolean deleteAudioFromSelfAlbum(String albumId,String audioId) {
        boolean isDelSucess = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        if (!TextUtils.isEmpty(albumId))
            where += " and album_id=" + albumId + "";
        try {
            isDelSucess = PHDbUtils.getInstance().delete(SelfAudioDB.class, where);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isDelSucess;
    }

    /**
     * 获取所有的自建专辑
     */
    public List<Album> getAllSelfAlbum() {
        List<Album> res = new ArrayList<>();
        String where = " 1=1 ";
        try {
            List<SelfAlbumDB> dbList = PHDbUtils.getInstance().query(SelfAlbumDB.class, where);
            if (dbList != null && dbList.size() > 0) {
                for (SelfAlbumDB db : dbList) {
                    Album album = new Album(new ArrayList(), db.getAuthor(), db.getCover(), db.getDesc(), db.getId(), 0, db.getName(),null);
                    res.add(album);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public List<Audio> getAudiosByAlbumId(String albumId) {
        List<Audio> res = new ArrayList<>();
        String where = " 1=1 ";
        try {
            if (!TextUtils.isEmpty(albumId)) {
                where += " and album_id=" + albumId + "";
                List<SelfAudioDB> audioDBS = PHDbUtils.getInstance().query(SelfAudioDB.class, where);
                if (audioDBS != null && audioDBS.size() > 0) {
                    for (SelfAudioDB db : audioDBS) {
                        Audio audio = new Audio(db.getId(), db.getAuthor(), db.getCover(), db.getDesc(), db.getEncrypt(), db.getName(), db.getUrl());
                        res.add(audio);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }


    /**
     * 保存 音频信息,配合{@link# saveAudioToAlbum(String, com.md.network.api.Audio)}使用
     *
     * @param audioId
     * @return
     */
    public boolean isAudioExist(String audioId) {
        boolean isAudioExist = false;
        String where = " 1=1 ";
        if (!TextUtils.isEmpty(audioId))
            where += " and id=" + audioId + "";
        try {
            List<SelfAudioDB> dbs = PHDbUtils.getInstance().query(SelfAudioDB.class, where);
            if (dbs == null || dbs.size() == 0) {
                isAudioExist = false;
            } else {
                isAudioExist = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isAudioExist;
    }


}
